package com.wanxi.controller;

import com.wanxi.model.ResultModel;
import com.wanxi.model.UserModel;
import com.wanxi.service.UserService;
import com.wanxi.service.impl.MyUserDetailsService;
import com.wanxi.util.JwtTokenUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.HashMap;

/**
 * @author 杨金乾
 */
@Controller
@CrossOrigin(origins = "*" )
@Slf4j
public class UserController {
    @Value("${jwt.tokenHead}")
    String tokenHead;
    @Autowired
    private UserService userService;
    @Autowired
    private JwtTokenUtil jwtTokenUtil;
    @Autowired
    SendMessageController sendMessageController;
    private String table="用户";
    @RequestMapping("/back/user/add")
    @ResponseBody
    @PreAuthorize("hasAuthority('allPermission')")
    protected ResultModel userAdd(UserModel model) throws IOException {

//        5.得到返回值:对于更新，返回的是影响的条数
        ResultModel resultModel = userService.add(model);
        sendMessageController.addUserMessages(table);
//        6.将值返回给前端
        return resultModel;
    }

    @RequestMapping("/back/user/delete")
    @ResponseBody
    @PreAuthorize("hasAuthority('allPermission')")
    protected ResultModel userDelete(UserModel model) throws IOException {

//        5.得到返回值
        ResultModel resultModel = userService.delete(model);
        sendMessageController.deleteUserMessages(table);
//        6.将值返回给前端
        return resultModel;
    }

    @RequestMapping("/back/user/findAll")
    @ResponseBody
    @PreAuthorize("hasAuthority('allPermission')")
    protected ResultModel userFindAll(UserModel model) throws IOException {

//        5.得到返回值
        ResultModel resultModel = userService.findAll(model);
        sendMessageController.findAllUserMessages(table);
//        6.将值返回给前端
        return resultModel;
    }
    @RequestMapping("/back/user/findById")
    @ResponseBody
    @PreAuthorize("hasAuthority('allPermission')")
    protected ResultModel userFindById(UserModel model) throws IOException {

//        5.得到返回值
        ResultModel resultModel = userService.findById(model);
        sendMessageController.findByIdUserMessages(table);
//        6.将值返回给前端
        return resultModel;
    }

    @RequestMapping("/back/user/update")
    @ResponseBody
    @PreAuthorize("hasAuthority('allPermission')")
    protected ResultModel userUpdate(UserModel model) throws IOException {

//        5.得到返回值
        ResultModel resultModel = userService.update(model);
        sendMessageController.updateUserMessages(table);
//        6.将值返回给前端
        return resultModel;
    }

    @RequestMapping("/back/user/updatePassword")
    @ResponseBody
    @PreAuthorize("hasAuthority('allPermission')")
    protected ResultModel userUpdatePassword(UserModel model) throws IOException {

//        5.得到返回值
        ResultModel resultModel = userService.password(model);
        sendMessageController.updatePasswordUserMessages(table);
//        6.将值返回给前端
        return resultModel;
    }

    @RequestMapping("/login")
    @ResponseBody
    /**
     * 告知SpringMVC框架 不进行视图跳转 直接进行数据响应
     */
    protected ResultModel login(UserModel model, HttpServletRequest request) throws IOException {
        HashMap<String, String> data = new HashMap<>();

        ResultModel resultModel = userService.login(model);
        //判定登录结果，如果登录成功，
        if ("success".equals(resultModel.getMsg())){


            sendMessageController.loginUserMessages(model);
            String username=model.getUsername();
            String token = jwtTokenUtil.generateToken(model);
            data.put("tokenHead",tokenHead);
            data.put("access_token",token);
            data.put("username", username);


//        6.返回给前端
            return ResultModel.getResult(data);
        }
        return resultModel;
    }
}
